home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / libraries / relation.lib.php < prev    next >
Encoding:
PHP Script  |  2004-08-16  |  29.6 KB  |  746 lines

  1. <?php
  2. /* $Id: relation.lib.php,v 2.23 2004/08/16 15:19:15 nijel Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4. error_reporting(E_ALL);
  5. /**
  6.  * Set of functions used with the relation and pdf feature
  7.  */
  8.  
  9. /**
  10.  * Executes a query as controluser if possible, otherwise as normal user
  11.  *
  12.  * @param   string    the query to execute
  13.  * @param   boolean   whether to display SQL error messages or not
  14.  *
  15.  * @return  integer   the result id
  16.  *
  17.  * @global  string    the URL of the page to show in case of error
  18.  * @global  string    the name of db to come back to
  19.  * @global  resource  the resource id of DB connect as controluser
  20.  * @global  array     configuration infos about the relations stuff
  21.  *
  22.  * @access  public
  23.  *
  24.  * @author  Mike Beck <mikebeck@users.sourceforge.net>
  25.  */
  26.  function PMA_query_as_cu($sql, $show_error = TRUE, $options = 0) {
  27.     global $err_url_0, $db, $dbh, $cfgRelation;
  28.  
  29.     PMA_DBI_select_db($cfgRelation['db'], $dbh);
  30.     if ($show_error) {
  31.         $result = PMA_DBI_query($sql, $dbh, $options);
  32.     } else {
  33.         $result = @PMA_DBI_try_query($sql, $dbh, $options);
  34.     } // end if... else...
  35.     PMA_DBI_select_db($db, $dbh);
  36.  
  37.     if ($result) {
  38.         return $result;
  39.     } else {
  40.         return FALSE;
  41.     }
  42.  } // end of the "PMA_query_as_cu()" function 
  43.  
  44.  
  45. /**
  46.  * Defines the relation parameters for the current user
  47.  * just a copy of the functions used for relations ;-)
  48.  * but added some stuff to check what will work
  49.  *
  50.  * @param   boolean  whether to check validity of settings or not
  51.  *
  52.  * @return  array    the relation parameters for the current user
  53.  *
  54.  * @global  array    the list of settings for servers
  55.  * @global  integer  the id of the current server
  56.  * @global  string   the URL of the page to show in case of error
  57.  * @global  string   the name of the current db
  58.  * @global  string   the name of the current table
  59.  * @global  array    configuration infos about the relations stuff
  60.  *
  61.  * @access  public
  62.  *
  63.  * @author  Mike Beck <mikebeck@users.sourceforge.net>
  64.  */
  65. function PMA_getRelationsParam($verbose = FALSE)
  66. {
  67.     global $cfg, $server, $err_url_0, $db, $table;
  68.     global $cfgRelation;
  69.  
  70.     $cfgRelation                = array();
  71.     $cfgRelation['relwork']     = FALSE;
  72.     $cfgRelation['displaywork'] = FALSE;
  73.     $cfgRelation['bookmarkwork']= FALSE;
  74.     $cfgRelation['pdfwork']     = FALSE;
  75.     $cfgRelation['commwork']    = FALSE;
  76.     $cfgRelation['mimework']    = FALSE;
  77.     $cfgRelation['historywork'] = FALSE;
  78.     $cfgRelation['allworks']    = FALSE;
  79.  
  80.     // No server selected -> no bookmark table
  81.     // we return the array with the FALSEs in it,
  82.     // to avoid some 'Unitialized string offset' errors later
  83.     if ($server == 0
  84.        || empty($cfg['Server'])
  85.        || empty($cfg['Server']['pmadb'])) {
  86.         if ($verbose == TRUE) {
  87.             echo 'PMA Database ... '
  88.                  . '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font>'
  89.                  . '[ <a href="Documentation.html#pmadb">' . $GLOBALS['strDocu'] . '</a> ]<br />' . "\n"
  90.                  . $GLOBALS['strGeneralRelationFeat']
  91.                  . ' <font color="green">' . $GLOBALS['strDisabled'] . '</font>' . "\n";
  92.         }
  93.         return $cfgRelation;
  94.     }
  95.  
  96.     $cfgRelation['user']  = $cfg['Server']['user'];
  97.     $cfgRelation['db']    = $cfg['Server']['pmadb'];
  98.  
  99.     //  Now I just check if all tables that i need are present so I can for
  100.     //  example enable relations but not pdf...
  101.     //  I was thinking of checking if they have all required columns but I
  102.     //  fear it might be too slow
  103.     // PMA_DBI_select_db($cfgRelation['db']);
  104.  
  105.     $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($cfgRelation['db']);
  106.     $tab_rs    = PMA_query_as_cu($tab_query, FALSE, PMA_DBI_QUERY_STORE);
  107.  
  108.     if ($tab_rs) {
  109.         while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
  110.             if ($curr_table[0] == $cfg['Server']['bookmarktable']) {
  111.                 $cfgRelation['bookmark']        = $curr_table[0];
  112.             } else if ($curr_table[0] == $cfg['Server']['relation']) {
  113.                 $cfgRelation['relation']        = $curr_table[0];
  114.             } else if ($curr_table[0] == $cfg['Server']['table_info']) {
  115.                 $cfgRelation['table_info']      = $curr_table[0];
  116.             } else if ($curr_table[0] == $cfg['Server']['table_coords']) {
  117.                 $cfgRelation['table_coords']    = $curr_table[0];
  118.             } else if ($curr_table[0] == $cfg['Server']['column_info']) {
  119.                 $cfgRelation['column_info'] = $curr_table[0];
  120.             } else if ($curr_table[0] == $cfg['Server']['pdf_pages']) {
  121.                 $cfgRelation['pdf_pages']       = $curr_table[0];
  122.             } else if ($curr_table[0] == $cfg['Server']['history']) {
  123.                 $cfgRelation['history'] = $curr_table[0];
  124.             }
  125.         } // end while
  126.     }
  127.  
  128.     if (isset($cfgRelation['relation'])) {
  129.         $cfgRelation['relwork']         = TRUE;
  130.         if (isset($cfgRelation['table_info'])) {
  131.                 $cfgRelation['displaywork'] = TRUE;
  132.         }
  133.     }
  134.     if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
  135.         $cfgRelation['pdfwork']     = TRUE;
  136.     }
  137.     if (isset($cfgRelation['column_info'])) {
  138.         $cfgRelation['commwork']    = TRUE;
  139.  
  140.         if ($cfg['Server']['verbose_check']) {
  141.             $mime_query  = 'SHOW FIELDS FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
  142.             $mime_rs     = PMA_query_as_cu($mime_query, FALSE);
  143.  
  144.             $mime_field_mimetype                = FALSE;
  145.             $mime_field_transformation          = FALSE;
  146.             $mime_field_transformation_options  = FALSE;
  147.             while ($curr_mime_field = @PMA_DBI_fetch_row($mime_rs)) {
  148.                 if ($curr_mime_field[0] == 'mimetype') {
  149.                     $mime_field_mimetype               = TRUE;
  150.                 } else if ($curr_mime_field[0] == 'transformation') {
  151.                     $mime_field_transformation         = TRUE;
  152.                 } else if ($curr_mime_field[0] == 'transformation_options') {
  153.                     $mime_field_transformation_options = TRUE;
  154.                 }
  155.             }
  156.             PMA_DBI_free_result($mime_rs);
  157.  
  158.             if ($mime_field_mimetype == TRUE
  159.                 && $mime_field_transformation == TRUE
  160.                 && $mime_field_transformation_options == TRUE) {
  161.                 $cfgRelation['mimework'] = TRUE;
  162.             }
  163.         } else {
  164.             $cfgRelation['mimework'] = TRUE;
  165.         }
  166.     }
  167.  
  168.     if (isset($cfgRelation['history'])) {
  169.         $cfgRelation['historywork']     = TRUE;
  170.     }
  171.  
  172.     if (isset($cfgRelation['bookmark'])) {
  173.         $cfgRelation['bookmarkwork']     = TRUE;
  174.     }
  175.  
  176.     if ($cfgRelation['relwork'] == TRUE && $cfgRelation['displaywork'] == TRUE
  177.         && $cfgRelation['pdfwork'] == TRUE && $cfgRelation['commwork'] == TRUE
  178.         && $cfgRelation['mimework'] == TRUE && $cfgRelation['historywork'] == TRUE
  179.         && $cfgRelation['bookmarkwork'] == TRUE) {
  180.         $cfgRelation['allworks'] = TRUE;
  181.     }
  182.     if ($tab_rs) {
  183.         PMA_DBI_free_result($tab_rs);
  184.     } else {
  185.         $cfg['Server']['pmadb'] = FALSE;
  186.     }
  187.  
  188.     if ($verbose == TRUE) {
  189.         $shit     = '<font color="red"><b>' . $GLOBALS['strNotOK'] . '</b></font> [ <a href="Documentation.html#%s">' . $GLOBALS['strDocu'] . '</a> ]';
  190.         $hit      = '<font color="green"><b>' . $GLOBALS['strOK'] . '</b></font>';
  191.         $enabled  = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
  192.         $disabled = '<font color="red">'   . $GLOBALS['strDisabled'] . '</font>';
  193.  
  194.         echo '<table>' . "\n";
  195.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'pmadb\'] ... </th><td align="right">'
  196.              . (($cfg['Server']['pmadb'] == FALSE) ? sprintf($shit, 'pmadb') : $hit)
  197.              . '</td></tr>' . "\n";
  198.         echo '    <tr><td> </td></tr>' . "\n";
  199.  
  200.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'relation\'] ... </th><td align="right">'
  201.              . ((isset($cfgRelation['relation'])) ? $hit : sprintf($shit, 'relation'))
  202.              . '</td></tr>' . "\n";
  203.         echo '    <tr><td colspan=2 align="center">'. $GLOBALS['strGeneralRelationFeat'] . ': '
  204.              . (($cfgRelation['relwork'] == TRUE) ? $enabled :  $disabled)
  205.              . '</td></tr>' . "\n";
  206.         echo '    <tr><td> </td></tr>' . "\n";
  207.  
  208.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'table_info\']   ... </th><td align="right">'
  209.              . (($cfgRelation['displaywork'] == FALSE) ? sprintf($shit, 'table_info') : $hit)
  210.              . '</td></tr>' . "\n";
  211.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strDisplayFeat'] . ': '
  212.              . (($cfgRelation['displaywork'] == TRUE) ? $enabled : $disabled)
  213.              . '</td></tr>' . "\n";
  214.         echo '    <tr><td> </td></tr>' . "\n";
  215.  
  216.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'table_coords\'] ... </th><td align="right">'
  217.              . ((isset($cfgRelation['table_coords'])) ? $hit : sprintf($shit, 'table_coords'))
  218.              . '</td></tr>' . "\n";
  219.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'pdf_pages\'] ... </th><td align="right">'
  220.              . ((isset($cfgRelation['pdf_pages'])) ? $hit : sprintf($shit, 'table_coords'))
  221.              . '</td></tr>' . "\n";
  222.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strCreatePdfFeat'] . ': '
  223.              . (($cfgRelation['pdfwork'] == TRUE) ? $enabled : $disabled)
  224.              . '</td></tr>' . "\n";
  225.         echo '    <tr><td> </td></tr>' . "\n";
  226.  
  227.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'column_info\'] ... </th><td align="right">'
  228.              . ((isset($cfgRelation['column_info'])) ? $hit : sprintf($shit, 'col_com'))
  229.              . '</td></tr>' . "\n";
  230.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strColComFeat'] . ': '
  231.              . (($cfgRelation['commwork'] == TRUE) ? $enabled : $disabled)
  232.              . '</td></tr>' . "\n";
  233.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strBookmarkQuery'] . ': '
  234.              . (($cfgRelation['bookmarkwork'] == TRUE) ? $enabled : $disabled)
  235.              . '</td></tr>' . "\n";
  236.         echo '    <tr><th align="left">MIME ...</th><td align="right">'
  237.              . (($cfgRelation['mimework'] == TRUE) ? $hit : sprintf($shit, 'col_com'))
  238.              . '</td></tr>' . "\n";
  239.  
  240.              if (($cfgRelation['commwork'] == TRUE) && ($cfgRelation['mimework'] != TRUE)) {
  241.                  echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
  242.              }
  243.  
  244.         echo '    <tr><th align="left">$cfg[\'Servers\'][$i][\'history\'] ... </th><td align="right">'
  245.              . ((isset($cfgRelation['history'])) ? $hit : sprintf($shit, 'history'))
  246.              . '</td></tr>' . "\n";
  247.         echo '    <tr><td colspan=2 align="center">' . $GLOBALS['strQuerySQLHistory'] . ': '
  248.              . (($cfgRelation['historywork'] == TRUE) ? $enabled : $disabled)
  249.              . '</td></tr>' . "\n";
  250.  
  251.         echo '</table>' . "\n";
  252.     } // end if ($verbose == TRUE) {
  253.  
  254.     return $cfgRelation;
  255. } // end of the 'PMA_getRelationsParam()' function
  256.  
  257.  
  258. /**
  259.  * Gets all Relations to foreign tables for a given table or
  260.  * optionally a given column in a table
  261.  *
  262.  * @param   string   the name of the db to check for
  263.  * @param   string   the name of the table to check for
  264.  * @param   string   the name of the column to check for
  265.  * @param   string   the source for foreign key information
  266.  *
  267.  * @return  array    db,table,column
  268.  *
  269.  * @global  array    the list of relations settings
  270.  * @global  string   the URL of the page to show in case of error
  271.  *
  272.  * @access  public
  273.  *
  274.  * @author  Mike Beck <mikebeck@users.sourceforge.net> and Marc Delisle
  275.  */
  276. function PMA_getForeigners($db, $table, $column = '', $source = 'both') {
  277.     global $cfgRelation, $err_url_0;
  278.  
  279.     if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
  280.         $rel_query          = 'SELECT master_field, foreign_db, foreign_table, foreign_field'
  281.                             . ' FROM ' . PMA_backquote($cfgRelation['relation']);
  282.         if (PMA_MYSQL_INT_VERSION >= 40100) {
  283.             list($conn_charset) = explode('_', $GLOBALS['collation_connection']);
  284.             $rel_query     .= ' WHERE CONVERT(master_db USING ' . $conn_charset . ') =  \'' . PMA_sqlAddslashes($db) . '\' '
  285.                             . ' AND   CONVERT(master_table USING ' . $conn_charset . ') = \'' . PMA_sqlAddslashes($table) . '\' ';
  286.             if (!empty($column)) {
  287.                 $rel_query .= ' AND   CONVERT(master_field USING ' . $conn_charset . ') = \'' . PMA_sqlAddslashes($column) . '\'';
  288.             }
  289.             unset($conn_charset);
  290.         } else {
  291.             $rel_query     .= ' WHERE master_db =  \'' . PMA_sqlAddslashes($db) . '\' '
  292.                             . ' AND   master_table = \'' . PMA_sqlAddslashes($table) . '\' ';
  293.             if (!empty($column)) {
  294.                 $rel_query .= ' AND   master_field = \'' . PMA_sqlAddslashes($column) . '\'';
  295.             }
  296.         }
  297.         $relations     = PMA_query_as_cu($rel_query);
  298.         $i = 0;
  299.         while ($relrow = PMA_DBI_fetch_assoc($relations)) {
  300.             $field                            = $relrow['master_field'];
  301.             $foreign[$field]['foreign_db']    = $relrow['foreign_db'];
  302.             $foreign[$field]['foreign_table'] = $relrow['foreign_table'];
  303.             $foreign[$field]['foreign_field'] = $relrow['foreign_field'];
  304.             $i++;
  305.         } // end while
  306.         PMA_DBI_free_result($relations);
  307.         unset($relations);
  308.     }
  309.  
  310.     if (($source == 'both' || $source == 'innodb') && !empty($table)) {
  311.         $show_create_table_query = 'SHOW CREATE TABLE '
  312.             . PMA_backquote($db) . '.' . PMA_backquote($table);
  313.         $show_create_table_res = PMA_DBI_query($show_create_table_query);
  314.         list(,$show_create_table) = PMA_DBI_fetch_row($show_create_table_res);
  315.         PMA_DBI_free_result($show_create_table_res);
  316.         unset($show_create_table_res, $show_create_table_query);
  317.         $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  318.  
  319.         foreach ($analyzed_sql[0]['foreign_keys'] AS $one_key) {
  320.  
  321.         // the analyzer may return more than one column name in the
  322.         // index list or the ref_index_list
  323.             foreach ($one_key['index_list'] AS $i => $field) {
  324.  
  325.         // If a foreign key is defined in the 'internal' source (pmadb)
  326.         // and in 'innodb', we won't get it twice if $source='both'
  327.         // because we use $field as key
  328.  
  329.                 // The parser looks for a CONSTRAINT clause just before
  330.                 // the FOREIGN KEY clause. It finds it (as output from
  331.                 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
  332.                 // versions like 3.23.58. 
  333.                 // In those cases, the FOREIGN KEY parsing will put numbers
  334.                 // like -1, 0, 1... instead of the constraint number.
  335.  
  336.                 if (isset($one_key['constraint'])) {
  337.                     $foreign[$field]['constraint'] = $one_key['constraint'];
  338.                 }
  339.  
  340.                 if (isset($one_key['ref_db_name'])) {
  341.                     $foreign[$field]['foreign_db']    = $one_key['ref_db_name'];
  342.                 } else {
  343.                     $foreign[$field]['foreign_db']    = $db;
  344.                 }
  345.                 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
  346.                 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
  347.                 if (isset($one_key['on_delete'])) {
  348.                     $foreign[$field]['on_delete'] = $one_key['on_delete'];
  349.                 }
  350.                 if (isset($one_key['on_update'])) {
  351.                     $foreign[$field]['on_update'] = $one_key['on_update'];
  352.                 }
  353.             }
  354.         }
  355.     }
  356.  
  357.     if (isset($foreign) && is_array($foreign)) {
  358.        return $foreign;
  359.     } else {
  360.        return FALSE;
  361.     }
  362. } // end of the 'PMA_getForeigners()' function
  363.  
  364.  
  365. /**
  366.  * Gets the display field of a table
  367.  *
  368.  * @param   string   the name of the db to check for
  369.  * @param   string   the name of the table to check for
  370.  *
  371.  * @return  string   field name
  372.  *
  373.  * @global  array    the list of relations settings
  374.  *
  375.  * @access  public
  376.  *
  377.  * @author  Mike Beck <mikebeck@users.sourceforge.net>
  378.  */
  379. function PMA_getDisplayField($db, $table) {
  380.     global $cfgRelation;
  381.     if (trim(@$cfgRelation['table_info']) == '') {
  382.         return FALSE;
  383.     }
  384.  
  385.     $disp_query      = 'SELECT display_field FROM ' . PMA_backquote($cfgRelation['table_info']);
  386.     if (PMA_MYSQL_INT_VERSION >= 40100) {
  387.         list($conn_charset) = explode('_', $GLOBALS['collation_connection']);
  388.         $disp_query .= ' WHERE CONVERT(db_name    USING ' . $conn_charset . ') = \'' . PMA_sqlAddslashes($db) . '\''
  389.                      . ' AND   CONVERT(table_name USING ' . $conn_charset . ') = \'' . PMA_sqlAddslashes($table) . '\'';
  390.     } else {
  391.         $disp_query .= ' WHERE db_name    = \'' . PMA_sqlAddslashes($db) . '\''
  392.                      . ' AND   table_name = \'' . PMA_sqlAddslashes($table) . '\'';
  393.     }
  394.  
  395.     $disp_res   = PMA_query_as_cu($disp_query);
  396.     $row        = ($disp_res ? PMA_DBI_fetch_assoc($disp_res) : '');
  397.     PMA_DBI_free_result($disp_res);
  398.     if (isset($row['display_field'])) {
  399.         return $row['display_field'];
  400.     } else {
  401.         return FALSE;
  402.     }
  403. } // end of the 'PMA_getDisplayField()' function
  404.  
  405.  
  406. /**
  407.  * Gets the comments for all rows of a table
  408.  *
  409.  * @param   string   the name of the db to check for
  410.  * @param   string   the name of the table to check for
  411.  *
  412.  * @return  array    [field_name] = comment
  413.  *
  414.  * @global  array    the list of relations settings
  415.  *
  416.  * @access  public
  417.  *
  418.  * @author  Mike Beck <mikebeck@users.sourceforge.net>
  419.  */
  420. function PMA_getComments($db, $table = '') {
  421.     global $cfgRelation;
  422.  
  423.     if (PMA_MYSQL_INT_VERSION >= 40100) {
  424.         list($conn_charset) = explode('_', $GLOBALS['collation_connection']);
  425.     }
  426.  
  427.     if ($table != '') {
  428.         $com_qry      = 'SELECT column_name, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info']);
  429.         if (PMA_MYSQL_INT_VERSION >= 40100) {
  430.             $com_qry .= ' WHERE CONVERT(db_name    USING ' . $conn_charset . ') = \'' . PMA_sqlAddslashes($db) . '\''
  431.                       . ' AND   CONVERT(table_name USING ' . $conn_charset . ') = \'' . PMA_sqlAddslashes($table) . '\'';
  432.         } else {
  433.             $com_qry .= ' WHERE db_name    = \'' . PMA_sqlAddslashes($db) . '\''
  434.                       . ' AND   table_name = \'' . PMA_sqlAddslashes($table) . '\'';
  435.         }
  436.         $com_rs   = PMA_query_as_cu($com_qry, TRUE);
  437.     } else {
  438.         $com_qry      = 'SELECT ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info']);
  439.         if (PMA_MYSQL_INT_VERSION >= 40100) {
  440.             $com_qry .= ' WHERE CONVERT(db_name     USING ' . $conn_charset . ') = \'' . PMA_sqlAddslashes($db) . '\''
  441.                       . ' AND   CONVERT(table_name  USING ' . $conn_charset . ') = \'\''
  442.                       . ' AND   CONVERT(column_name USING ' . $conn_charset . ') = \'(db_comment)\'';
  443.         } else {
  444.             $com_qry .= ' WHERE db_name     = \'' . PMA_sqlAddslashes($db) . '\''
  445.                       . ' AND   table_name  = \'\''
  446.                       . ' AND   column_name = \'(db_comment)\'';
  447.         }
  448.         $com_rs   = PMA_query_as_cu($com_qry, TRUE);
  449.     }
  450.  
  451.     $i = 0;
  452.     while ($row = PMA_DBI_fetch_assoc($com_rs)) {
  453.         $i++;
  454.         $col           = ($table != '' ? $row['column_name'] : $i);
  455.  
  456.         if (strlen($row['comment']) > 0) {
  457.             $comment[$col] = $row['comment'];
  458.         }
  459.  
  460.     } // end while
  461.     PMA_DBI_free_result($com_rs);
  462.     unset($com_rs);
  463.  
  464.     if (isset($comment) && is_array($comment)) {
  465.         return $comment;
  466.      } else {
  467.         return FALSE;
  468.      }
  469.  } // end of the 'PMA_getComments()' function
  470.  
  471. /**
  472. * Adds/removes slashes if required
  473. *
  474. * @param   string  the string to slash
  475. *
  476. * @return  string  the slashed string
  477. *
  478. * @access  public
  479. */
  480. function PMA_handleSlashes($val) {
  481.   return (get_magic_quotes_gpc() ? str_replace('\\"', '"', $val) : PMA_sqlAddslashes($val));
  482. } // end of the "PMA_handleSlashes()" function
  483.  
  484. /**
  485. * Set a single comment to a certain value.
  486. *
  487. * @param   string   the name of the db
  488. * @param   string   the name of the table
  489. * @param   string   the name of the column
  490. * @param   string   the value of the column
  491. * @param   string   (optional) if a column is renamed, this is the name of the former key which will get deleted
  492. *
  493. * @return  boolean  true, if comment-query was made.
  494. *
  495. * @global  array    the list of relations settings
  496. *
  497. * @access  public
  498. */
  499. function PMA_setComment($db, $table, $key, $value, $removekey = '') {
  500.     global $cfgRelation;
  501.  
  502.     if (PMA_MYSQL_INT_VERSION >= 40100) {
  503.         list($conn_charset) = explode('_', $GLOBALS['collation_connection']);
  504.         $cols = array(
  505.             'db_name'     => 'CONVERT(db_name     USING ' . $conn_charset . ')',
  506.             'table_name'  => 'CONVERT(table_name  USING ' . $conn_charset . ')',
  507.             'column_name' => 'CONVERT(column_name USING ' . $conn_charset . ')'
  508.         );
  509.     } else {
  510.         $cols = array(
  511.             'db_name'     => 'db_name    ',
  512.             'table_name'  => 'table_name ',
  513.             'column_name' => 'column_name'
  514.         );
  515.     }
  516.  
  517.     if ($removekey != '' AND $removekey != $key) {
  518.         $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
  519.                       . ' WHERE ' . $cols['db_name']     . ' = \'' . PMA_sqlAddslashes($db) . '\''
  520.                       . ' AND   ' . $cols['table_name']  . ' = \'' . PMA_sqlAddslashes($table) . '\''
  521.                       . ' AND   ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($removekey) . '\'';
  522.         PMA_query_as_cu($remove_query);
  523.         unset($remove_query);
  524.     }
  525.  
  526.     $test_qry = 'SELECT ' . PMA_backquote('comment') . ', mimetype, transformation, transformation_options FROM ' . PMA_backquote($cfgRelation['column_info'])
  527.               . ' WHERE ' . $cols['db_name']     . ' = \'' . PMA_sqlAddslashes($db) . '\''
  528.               . ' AND   ' . $cols['table_name']  . ' = \'' . PMA_sqlAddslashes($table) . '\''
  529.               . ' AND   ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($key) . '\'';
  530.     $test_rs   = PMA_query_as_cu($test_qry, TRUE, PMA_DBI_QUERY_STORE);
  531.  
  532.     if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
  533.         $row = PMA_DBI_fetch_assoc($test_rs);
  534.         PMA_DBI_free_result($test_rs);
  535.  
  536.         if (strlen($value) > 0 || strlen($row['mimetype']) > 0 || strlen($row['transformation']) > 0 || strlen($row['transformation_options']) > 0) {
  537.             $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
  538.                        . ' SET ' . PMA_backquote('comment') . ' = \'' . PMA_sqlAddslashes($value) . '\''
  539.                        . ' WHERE ' . $cols['db_name']     . ' = \'' . PMA_sqlAddslashes($db) . '\''
  540.                        . ' AND   ' . $cols['table_name']  . ' = \'' . PMA_sqlAddslashes($table) . '\''
  541.                        . ' AND   ' . $cols['column_name'] . ' = \'' . PMA_sqlAddSlashes($key) . '\'';
  542.         } else {
  543.             $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
  544.                        . ' WHERE ' . $cols['db_name']     . ' = \'' . PMA_sqlAddslashes($db) . '\''
  545.                        . ' AND   ' . $cols['table_name']  . ' = \'' . PMA_sqlAddslashes($table) . '\''
  546.                        . ' AND   ' . $cols['column_name'] . ' = \'' . PMA_sqlAddslashes($key) . '\'';
  547.         }
  548.     } else if (strlen($value) > 0) {
  549.         $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_info'])
  550.                    . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ') '
  551.                    . ' VALUES('
  552.                    . '\'' . PMA_sqlAddslashes($db) . '\','
  553.                    . '\'' . PMA_sqlAddslashes($table) . '\','
  554.                    . '\'' . PMA_sqlAddslashes($key) . '\','
  555.                    . '\'' . PMA_sqlAddslashes($value) . '\')';
  556.     }
  557.  
  558.     if (isset($upd_query)){
  559.         $upd_rs    = PMA_query_as_cu($upd_query);
  560.         unset($upd_query);
  561.         return true;
  562.     } else {
  563.         return false;
  564.     }
  565. } // end of 'PMA_setComment()' function
  566.  
  567. /**
  568. * Set a SQL history entry
  569. *
  570. * @param   string   the name of the db
  571. * @param   string   the name of the table
  572. * @param   string   the username
  573. * @param   string   the sql query
  574. *
  575. * @return  boolean  true
  576. *
  577. * @access  public
  578. */
  579. function PMA_setHistory($db, $table, $username, $sqlquery) {
  580.     global $cfgRelation;
  581.  
  582.     $hist_rs    = PMA_query_as_cu('INSERT INTO ' . PMA_backquote($cfgRelation['history']) . ' ('
  583.                 . PMA_backquote('username') . ','
  584.                 . PMA_backquote('db') . ','
  585.                 . PMA_backquote('table') . ','
  586.                 . PMA_backquote('timevalue') . ','
  587.                 . PMA_backquote('sqlquery')
  588.                 . ') VALUES ('
  589.                 . '\'' . PMA_sqlAddslashes($username) . '\','
  590.                 . '\'' . PMA_sqlAddslashes($db) . '\','
  591.                 . '\'' . PMA_sqlAddslashes($table) . '\','
  592.                 . 'NOW(),'
  593.                 . '\'' . PMA_sqlAddslashes($sqlquery) . '\')');
  594.     return true;
  595. } // end of 'PMA_setHistory()' function
  596.  
  597. /**
  598. * Gets a SQL history entry
  599. *
  600. * @param   string   the username
  601. *
  602. * @return  array    list of history items
  603. *
  604. * @access  public
  605. */
  606. function PMA_getHistory($username) {
  607.     global $cfgRelation;
  608.  
  609.     $hist_query      = 'SELECT '
  610.                      . PMA_backquote('db') . ','
  611.                      . PMA_backquote('table') . ','
  612.                      . PMA_backquote('sqlquery')
  613.                      . ' FROM ' . PMA_backquote($cfgRelation['history'])
  614.                      . ' WHERE ';
  615.     if (PMA_MYSQL_INT_VERSION >= 40100) {
  616.         list($conn_charset) = explode('_', $GLOBALS['collation_connection']);
  617.         $hist_query .= 'CONVERT(username USING ' . $conn_charset . ')';
  618.         unset($conn_charset);
  619.     } else {
  620.         $hist_query .= 'username';
  621.     }
  622.     $hist_query     .= ' = \'' . PMA_sqlAddslashes($username) . '\' ORDER BY id DESC';
  623.  
  624.     $hist_rs = PMA_query_as_cu($hist_query);
  625.     unset($hist_query);
  626.  
  627.     $history = array();
  628.  
  629.     while ($row = PMA_DBI_fetch_assoc($hist_rs)) {
  630.         $history[] = $row;
  631.     }
  632.     PMA_DBI_free_result($hist_rs);
  633.  
  634.     return $history;
  635.  
  636. } // end of 'PMA_getHistory()' function
  637.  
  638. /**
  639. * Set a SQL history entry
  640. *
  641. * @param   string   the name of the db
  642. * @param   string   the name of the table
  643. * @param   string   the username
  644. * @param   string   the sql query
  645. *
  646. * @return  boolean  true
  647. *
  648. * @access  public
  649. */
  650. function PMA_purgeHistory($username) {
  651.     global $cfgRelation, $cfg;
  652.  
  653.     $purge_query  = 'SELECT timevalue FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE ';
  654.     if (PMA_MYSQL_INT_VERSION >= 40100) {
  655.         list($conn_charset) = explode('_', $GLOBALS['collation_connection']);
  656.         $purge_query .= 'CONVERT(username USING ' . $conn_charset . ')';
  657.         unset($conn_charset);
  658.     } else {
  659.         $purge_query .= 'username';
  660.     }
  661.     $purge_query .= ' = \'' . PMA_sqlAddSlashes($username) . '\' ORDER BY timevalue DESC LIMIT ' . $cfg['QueryHistoryMax'] . ', 1';
  662.     $purge_rs = PMA_query_as_cu($purge_query);
  663.     $i = 0;
  664.     $row = PMA_DBI_fetch_row($purge_rs);
  665.     PMA_DBI_free_result($purge_rs);
  666.  
  667.     if (is_array($row) && isset($row[0]) && $row[0] > 0) {
  668.         $maxtime = $row[0];
  669.         // quotes added around $maxtime to prevent a difficult to
  670.         // reproduce problem
  671.         $remove_rs = PMA_query_as_cu('DELETE FROM ' . PMA_backquote($cfgRelation['history']) . ' WHERE timevalue <= "' . $maxtime . '"');
  672.     }
  673.  
  674.     return true;
  675. } // end of 'PMA_purgeHistory()' function
  676.  
  677. /**
  678. * Outputs dropdown with values of foreign fields
  679. *
  680. * @param   string   the query of the foreign keys
  681. * @param   string   the foreign field
  682. * @param   string   the foreign field to display
  683. * @param   string   the current data of the dropdown
  684. *
  685. * @return  string   the <option value=""><option>s
  686. *
  687. * @access  public
  688. */
  689. function PMA_foreignDropdown($disp, $foreign_field, $foreign_display, $data, $max = 100) {
  690.     global $cfg;
  691.  
  692.     $ret = '<option value=""></option>' . "\n";
  693.  
  694.     $reloptions = array('content-id' => array(), 'id-content' => array());
  695.     
  696.     foreach ($disp AS $disp_key => $relrow) {
  697.         $key   = $relrow[$foreign_field];
  698.  
  699.         // if the display field has been defined for the foreign table
  700.         if ($foreign_display) {
  701.             if (PMA_strlen($relrow[$foreign_display]) <= $cfg['LimitChars']) {
  702.                 $value  = htmlspecialchars($relrow[$foreign_display]);
  703.                 $vtitle = '';
  704.             } else {
  705.                 $vtitle = htmlspecialchars($relrow[$foreign_display]);
  706.                 $value  = htmlspecialchars(substr($vtitle, 0, $cfg['LimitChars']) . '...');
  707.             }
  708.         } else {
  709.             $vtitle = $value = '';
  710.         } // end if ($foreign_display)
  711.  
  712.         $reloption = '<option value="' . htmlspecialchars($key) . '"';
  713.         if ($vtitle != '') {
  714.             $reloption .= ' title="' . $vtitle . '"';
  715.         }
  716.  
  717.         if ($key == $data) {
  718.            $reloption .= ' selected="selected"';
  719.         } // end if
  720.  
  721.         $reloptions['id-content'][] = $reloption . '>' . $value . ' - ' . htmlspecialchars($key) .  '</option>' . "\n";
  722.         $reloptions['content-id'][] = $reloption . '>' . htmlspecialchars($key) .  ' - ' . $value . '</option>' . "\n";
  723.     } // end while
  724.  
  725.     // the list of keys looks better if not sorted by description
  726.     if ($cfg['NaturalOrder']) {
  727.         natsort($reloptions['content-id']); }
  728.     else {
  729.         asort($reloptions['content-id']);
  730.     }
  731.  
  732.     if ($max == -1 || count($reloptions['content-id']) < $max) {
  733.         $ret .= implode('', $reloptions['content-id']);
  734.         if (count($reloptions['content-id']) > 0) {
  735.             $ret .= '<option value=""></option>' . "\n";
  736.             $ret .= '<option value=""></option>' . "\n";
  737.         }
  738.     }
  739.  
  740.     $ret .= implode('', $reloptions['id-content']);
  741.  
  742.     return $ret;
  743. } // end of 'PMA_foreignDropdown()' function
  744.  
  745. ?>
  746.